
shortcut.add("Page_up",function() {

if(document.getElementById('blanket2').style.display == "block"){
document.getElementById('blanket2').style.display = "none";
document.getElementById('popUpDiv2').style.display = "none";
}
document.getElementById('blanket').style.display = "block";
document.getElementById('popUpDiv').style.display = "block";
 

	
});
 
shortcut.add("Page_down",function() {
	
	if(document.getElementById('blanket').style.display == "block"){
document.getElementById('blanket').style.display = "none";
document.getElementById('popUpDiv').style.display = "none";
}
document.getElementById('blanket2').style.display = "block";
document.getElementById('popUpDiv2').style.display = "block";
	
});


shortcut.add("Alt+C",function() {
	
	newwindow = window.open('calculator','targetWindow','toolbar=no,location=no,top=200,status=no,menubar=no,scrollbars=yes,resizable=no,width=450,height=380');
	
});
function Close(){
	document.getElementById("popUpDiv").style.display = 'none';
	document.getElementById("blanket").style.display = 'none';
	document.getElementById("popUpDiv2").style.display = 'none';
	document.getElementById("blanket2").style.display = 'none';
}

var x;

function changecolors() {
    x = 1;
    setInterval(change, 1000);
}

function change() {
	var m=0;
var n=0;
var profitloss=0;
 $.get('./countProfitLoss',function(responseJson) {
 for(var a in responseJson){
	 m=m+(Number(responseJson[a]["lot1"])*Number(responseJson[a]["price1"]));
	 n=n+(Number(responseJson[a]["lot1"])*Number(responseJson[a]["price2"]));
	 profitloss=m-n;
 }

 if(profitloss>0){
	 document.getElementById('profit').style.background = "green";
	 document.getElementById("profit").value="Profit: "+profitloss;
	 
 }
 else{
	 document.getElementById('profit').style.background = "red";
	 document.getElementById("profit").value="Loss: "+profitloss;
	 	 
 }
 
 });
   
}

/*------------------------------------------Change color end------------------*/
function textsizer(e){

var evtobj=window.event? event : e; 
var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode;
var actualkey=String.fromCharCode(unicode);
if (actualkey=="Page_up"){
if(document.getElementById('blanket2').style.display == "block"){
document.getElementById('blanket2').style.display = "none";
document.getElementById('popUpDiv2').style.display = "none";
}
document.getElementById('blanket').style.display = "block";
document.getElementById('popUpDiv').style.display = "block";

}
if (actualkey=="Page_down"){
if(document.getElementById('blanket').style.display == "block"){
document.getElementById('blanket').style.display = "none";
document.getElementById('popUpDiv').style.display = "none";
}
document.getElementById('blanket2').style.display = "block";
document.getElementById('popUpDiv2').style.display = "block";
}
}
document.onkeypress=textsizer;

function BuySell(parameter){

if(parameter=='buy'){
document.getElementById('blanket').style.display = "block";
document.getElementById('popUpDiv').style.display = "block";
}
if(parameter=='sell'){
document.getElementById('blanket2').style.display = "block";
document.getElementById('popUpDiv2').style.display = "block";
}
}

function displayunicode(e){
changecolors();
var unicode=e.keyCode? e.keyCode : e.charCode;

}

$(document).ready( function() {
 done();
 
 $('#userId').change(function(event) {  
 	
	    var $userId=$("select#userId").val();
	   $.get('./userAccountDetail',{userName:$userId},function(responseJson) {   
	    	   
	    	   for(var a in responseJson){
					 document.getElementById("cashl").value=responseJson[a]["cashl"];
						document.getElementById("accnum").value=responseJson[a]["accno"];
						document.getElementById("marginl").value=responseJson[a]["marginl"];
						document.getElementById("marginb1").value=responseJson[a]["margin_b"];
	      }
	       });
	    });
 
 $('#userId1').change(function(event) {  
 	
	    var $userId=$("select#userId1").val();
	    $.get('./userAccountDetail',{userName:$userId},function(responseJson) {   
	    	    for(var a in responseJson){
	    			document.getElementById("cashl2").value=responseJson[a]["cashl"];
				document.getElementById("accnum2").value=responseJson[a]["accno"];
				document.getElementById("marginl2").value=responseJson[a]["marginl"];
				document.getElementById("marginb2").value=responseJson[a]["margin_b"];
	     
	    	   }
	       });
	    });
 
 $.get('./userList',function(responseJson) {
		
		var select = document.getElementById("userId");
			var option = document.createElement("option");
			select.appendChild(option);
	            if(responseJson!=null){      	
	                 for(var i=0; i<responseJson.length; i++ ){  
	      
			            			var option = document.createElement("option");
			            			option.value =responseJson[i]['user_ID'] ;
			            			
			            	        option.textContent = responseJson[i]['user_ID'];
			            		     select.appendChild(option);
						         }
	            } 
	      });
 
 $.get('./userList',function(responseJson) {
		var select = document.getElementById("userId1");
		var option = document.createElement("option");
		select.appendChild(option);
         if(responseJson!=null){      	
              for(var i=0; i<responseJson.length; i++ ){  
   							var option = document.createElement("option");
		            			option.value =responseJson[i]['user_ID'] ;
		            	        option.textContent = responseJson[i]['user_ID'];
		            		     select.appendChild(option);
					  }
         } 
   });
 
 $(submit).click(function(event) { 
		
		var msg=document.getElementById("msg").innerHTML;
		
	 
	if($("#ptype").val()=="Intra"){

		var x=$("#userId").val();
		if(x!="" && x!=null){
			var a=parseInt($("#marginb1").val());
			var b=parseInt($("#price").val());
			var c=parseInt($("#ql").val());
			var d=c*b;
	
			if(a>d)
			{
				msg+="You have buyed "+$("#ql").val()+" lots of "+$("#symbol").val()+" for "+$("#userId").val()+".<br/>";
				document.getElementById("msg").innerHTML=msg;
				var reqUrl="./broker/buyOrder";
				var buyJson=JSON.stringify({ "otype":$("#otype").val(),"insname":$("#insname").val(),"symbol":$("#symbol").val(),
					"expdate":$("#expdate").val(),"qt":$("#qt").val(),"ql":$("#ql").val(),"qw":$("#qw").val(),
					"price":$("#price").val(),"ptype":$("#ptype").val(),"status":$("#status").val(),"tp":$("#tp").val(),
					"userId":$("#userId").val(),"accno":$("#accnum").val(),"marginl":$("#marginl").val(),
					"cashl":$("#cashl").val(),"margin_b":$("#marginb1").val()});	
			
				$.ajax(
				  {
				  type: "POST",
				  url: reqUrl,
				  dataType: "json",
				  contentType: 'application/json',
				  data: buyJson,
				  success: function(html) { 			 
				    alert(html);
				  }
				});
			}
			else{
				alert("You have insufficient Margin Limit,Hence Can't buy.");
				 msg+=$("#userId").val()+" have insufficient margin limit hence can't buy "+$("#ql").val()+" lots of "+$("#symbol").val()+".<br/>";
					document.getElementById("msg").innerHTML=msg;
			}
		}
	else{
	
		var e=parseInt($("#marginb").val());
		var f=parseInt($("#price").val());
		var g=parseInt($("#ql").val());
		var h=f*g;

		if(e>h)
		{msg+="You have buyed "+$("#ql").val()+" lots of "+$("#symbol").val()+".<br/>";
		document.getElementById("msg").innerHTML=msg;
			var reqUrl="./broker/buyOrder";
			var buyJson1=JSON.stringify({ "otype":$("#otype").val(),"insname":$("#insname").val(),"symbol":$("#symbol").val(),"expdate":$("#expdate").val(),"qt":$("#qt").val(),"ql":$("#ql").val(),"qw":$("#qw").val(),"price":$("#price").val(),"ptype":$("#ptype").val(),"status":$("#status").val(),"tp":$("#tp").val(),"accno":$("#accno").val(),"marginl":$("#marginlimit").val(),"cashl":$("#cashlimit").val(),"margin_b":$("#marginb").val(),"userId":$("#userId").val()});
			$.ajax(
				  {
				  type: "POST",
				  url: reqUrl,
				  dataType: "json",
				  contentType: 'application/json',
				  data: buyJson1,
				  success: function(html) { 			 
				    alert(html);
				  }
				});
			}
		else{
			alert("You have insufficient Margin Limit,Hence Can't buy.");
			 msg+="You have insufficient margin limit hence can't buy "+$("#ql").val()+" lots of "+$("#symbol").val()+".<br/>";
				document.getElementById("msg").innerHTML=msg;
		}
	
	
		}
		
	}
	
	 if($("#ptype").val()=="CarryForward")
		{
		
			var x=$("#userId").val();
			if(x!="" && x!=null){
				msg+="You have buyed "+$("#ql").val()+" lots of "+$("#symbol").val()+" for "+$("#userId").val()+".<br/>";
				document.getElementById("msg").innerHTML=msg;
				var i=parseInt($("#cashl").val());
				var j=parseInt($("#price").val());
				var k=parseInt($("#ql").val());
				var l=j*k;
				if(i>l)
				{
					var reqUrl="./broker/buyOrder";
					var buyJson2=JSON.stringify({ "otype":$("#otype").val(),"insname":$("#insname").val(),"symbol":$("#symbol").val(),"expdate":$("#expdate").val(),"qt":$("#qt").val(),"ql":$("#ql").val(),"qw":$("#qw").val(),"price":$("#price").val(),"ptype":$("#ptype").val(),"status":$("#status").val(),"tp":$("#tp").val(),"userId":$("#userId").val(),"accno":$("#accnum").val(),"marginl":$("#marginl").val(),"cashl":$("#cashl").val(),"margin_b":$("#marginb1").val()});	
					$.ajax(
					  {
					  type: "POST",
					  url: reqUrl,
					  dataType: "json",
					  contentType: 'application/json',
					  data: buyJson2,
					  success: function(html) { 			 
					    alert(html);
					  }
					});
				}
				else{
					alert("You have insufficient Cash Limit,Hence Can't buy.");
					msg+=$("#userId").val()+" have insufficient margin limit hence can't buy "+$("#ql").val()+" lots of "+$("#symbol").val()+".<br/>";
					document.getElementById("msg").innerHTML=msg;
				}
			}
			else{
				var m=parseInt($("#cashlimit").val());
				var n=parseInt($("#price").val());
				var o=parseInt($("#ql").val());
				var p=n*o;

				if(m>p)
				{
					msg+="You have buyed "+$("#ql").val()+" lots of "+$("#symbol").val()+".<br/>";
					document.getElementById("msg").innerHTML=msg;
				var reqUrl="./broker/buyOrder";
				var buyJson3=JSON.stringify({ "otype":$("#otype").val(),"insname":$("#insname").val(),"symbol":$("#symbol").val(),"expdate":$("#expdate").val(),"qt":$("#qt").val(),"ql":$("#ql").val(),"qw":$("#qw").val(),"price":$("#price").val(),"ptype":$("#ptype").val(),"status":$("#status").val(),"tp":$("#tp").val(),"userId":$("#userId").val(),"accno":$("#accno").val(),"marginl":$("#marginlimit").val(),"cashl":$("#cashlimit").val(),"margin_b":$("#marginb").val()});
				$.ajax(
					  {
					  type: "POST",
					  url: reqUrl,
					  dataType: "json",
					  contentType: 'application/json',
					  data: buyJson3,
					  success: function(html) { 			 
					    alert(html);
					  }
					});
				}
				else{
					alert("You have insufficient Cash Limit,Hence Can't buy.");
					 msg+="You have insufficient margin limit hence can't buy "+$("#ql").val()+" lots of "+$("#symbol").val()+".<br/>";
						document.getElementById("msg").innerHTML=msg;
				}
		
			}
			
		}
		createUser1();
		});
 
 $(save).click(function(event) {
		
		var msg=document.getElementById("msgS").innerHTML;
	if($("#userId1").val()!="" && $("#userId1").val()!=null){
		var m=0,totalLots=0;
		var $userId=$("#userId1").val();
		 $.get('./broker/userBuyDetail',{userName:$userId},function(responseJson) {
				for(var a in responseJson){
	 				if(responseJson[a]["ptype"]==$("#ptype1").val() && responseJson[a]["symbol"]==$("#symbol").val()){
	 					totalLots=Number(responseJson[a]["lot1"])-Number(responseJson[a]["lot2"]);
						if(totalLots>=Number($("#ql1").val()))
						{
	 						m=1;
	 						break;
	 					}
	 				}
				}
	
	 if(m==1){
		 msg+=$userId+" has sold "+$("#ql").val()+" lots of "+$("#symbol").val()+"<br/>";
			document.getElementById("msgS").innerHTML=msg;
	var reqUrl="./broker/sellOrder";
	var sellJson= JSON.stringify({ "otype":$("#otype1").val(),"insname":$("#insname").val(),"symbol":$("#symbol").val(),
		"expdate":$("#expdate").val(),"qt":$("#qt").val(),"ql":$("#ql1").val(),"qw":$("#qw").val(),"price":$("#sprice").val(),
		"ptype":$("#ptype1").val(),"status":$("#status1").val(),"tp":$("#tp").val(),"userId":$userId,
		"accno":$("#accnum2").val(),"marginl":$("#marginl2").val(),"cashl":$("#cashl2").val(),"margin_b":$("#marginb2").val()});

	$.ajax(
		  {
		  type: "POST",
		  url: reqUrl,
		  dataType: "json",
		  contentType: 'application/json',
		  data:sellJson,
		  success: function(html) { 			 
		    alert(html);
		  }
	});
		}
	else{
				alert("User has not buyed sufficient lots of particular symbol");
				 msg+=$userId+" has insufficient lots buyed hence can't sell "+$("#ql1").val()+" lots of "+$("#symbol").val()+".<br/>";
					document.getElementById("msgS").innerHTML=msg;
			}
		 });
	}
	else{
		var m=0,totalLots=0;
		 $.get('./broker/brokerBuyDetail',function(responseJson) {

				for(var a in responseJson){

	 				if(responseJson[a]["ptype"]==$("#ptype1").val() && responseJson[a]["symbol"]==$("#symbol").val()){
	 					totalLots=Number(responseJson[a]["lot1"])-Number(responseJson[a]["lot2"]);
						if(totalLots>=Number($("#ql1").val()))
						{	m=1;
	 						break;
	 					}
	 				}
				}
				
	 if(m==1){
		 msg+="You have sold "+$("#ql").val()+" lots of "+$("#symbol").val()+"<br/>";
			document.getElementById("msgS").innerHTML=msg;
		var reqUrl="./broker/sellOrder";
			var sellJson= JSON.stringify({ "otype":$("#otype1").val(),"insname":$("#insname").val(),"symbol":$("#symbol").val(),
				"expdate":$("#expdate").val(),"qt":$("#qt").val(),"ql":$("#ql1").val(),"qw":$("#qw").val(),"userId":$("#userId1").val(),
				"price":$("#sprice").val(),"ptype":$("#ptype1").val(),"status":$("#status1").val(),"tp":$("#tp").val(),
				"accno":$("#accno1").val(),"marginl":$("#marginl1").val(),"cashl":$("#cashl1").val(),
				"margin_b":$("#marginb3").val()});
			$.ajax(
				  {
				  type: "POST",
				  url: reqUrl,
				  dataType: "json",
				  contentType: 'application/json',
				  data:sellJson,
				  success: function(html) { 			 
				    alert(html);
				  }
				});
	 }
	 else{
				alert("You have not buyed sufficient lots of particular symbol");
				 msg+="You have insufficient lots buyed hence can't sell "+$("#ql1").val()+" lots of "+$("#symbol").val()+".<br/>";
					document.getElementById("msgS").innerHTML=msg;
			}
		 });

		}
		createUser2();
	});
 
 $.ajax({
		url : './mock/brokerpage',
		dataType: "json",
		contentType: 'application/json',			  
		 success : function(jsonData) {
		for(var a in jsonData){
			document.getElementById("brokerId").value="Client ID: "+jsonData[a]["brokerId"];
				document.getElementById("fname").value="Client Name: "+jsonData[a]["fname"];	
			}
		}
	});
 
 done1();
 
 $("#notifyMe").click(function(event) {
	
	 document.getElementById("Alertmsg").innerHTML="";
			$.ajax({ 
				type: "POST",
			url : './updateStatus1',
			dataType: "json",
			contentType: 'application/json',			  
			success : function(jsonData) {
				var msg=document.getElementById("Alertmsg").innerHTML,i=1;
				document.getElementById("alert").innerHTML=" ";
				$( "span" ).removeClass( "mes" );
				for(var a in jsonData ){
		        	msg=msg+"("+i+").  "+jsonData[a]["msg"]+".<br/>";
					document.getElementById("Alertmsg").innerHTML=msg;
					i++;
				}
			}
			});
 });
 

 
});

function done1() {
	  setTimeout( function() { 
	  updates1(); 
	  done1();
	  }, 7000);
}

function updates1() {  	
	 $.ajax({
			url : './rmsNews',
			dataType: "json",
			contentType: 'application/json',			  
			success : function(jsonData) {
				var i=0;
				for(var a in jsonData ){
					var r=confirm("You have a notification: "+jsonData[a]["msg"]);
						if (r == true) {
				        updateStatus(jsonData[a]["idStatus"]);
				        }
						else {
				     		i++;
				    	} 
				}
				if(i==0){
					$( "span" ).removeClass( "mes" );
					document.getElementById("alert").innerHTML="";	
				}
				
				else{
					$( "span" ).last().addClass( "mes" );
					document.getElementById("alert").innerHTML="  "+i+"  ";
				}
	        	
			}
	   			
		});

} 

function updateStatus(a){
	msg=JSON.stringify({ "idNum":a});
	$.ajax({ 
		type: "POST",
	url : './updateStatus',
	dataType: "json",
	contentType: 'application/json',			  
	  data:msg,
	success : function(jsonData) {	
	}
	});
	
}



function done() {
	  setTimeout( function() { 
	  updates(); 
	  done();
	  }, 200);
}
function updates() {  	
	 $.get('./brokerAccountDetail',function(responseJson) {
	
			if(responseJson!=null){
			for(var a in responseJson){
					document.getElementById("cashlimit").value=responseJson[a]["cashl"];
					document.getElementById("accno").value=responseJson[a]["accno"];
					document.getElementById("accno1").value=responseJson[a]["accno"];
					document.getElementById("cashl1").value=responseJson[a]["cashl"];
					document.getElementById("marginlimit").value=responseJson[a]["marginl"];
					document.getElementById("marginl1").value=responseJson[a]["marginl"];
					document.getElementById("marginb").value=responseJson[a]["margin_b"];
					document.getElementById("marginb3").value=responseJson[a]["margin_b"];
			}
			}
			});		
}  


function createUser1(){
	$.get('./userList',function(responseJson) {
		var select = document.getElementById("userId");
			 $('#userId option[value!="0"]').remove();
			var option = document.createElement("option");
			select.appendChild(option);
	            if(responseJson!=null){      	
	                 for(var i=0; i<responseJson.length; i++ ){  
	      
			            			var option = document.createElement("option");
			            			option.value =responseJson[i]['user_ID'] ;
			            	        option.textContent = responseJson[i]['user_ID'];
			            		     select.appendChild(option);
						        
				
						  }
	            } 
	      });
}

	
	function createUser2(){
		$.get('./userList',function(responseJson) {
				var select = document.getElementById("userId1");
				 $('#userId1 option[value!="0"]').remove();
				var option = document.createElement("option");
				select.appendChild(option);
		            if(responseJson!=null){      	
		                 for(var i=0; i<responseJson.length; i++ ){  
										var option = document.createElement("option");
				            			option.value =responseJson[i]['user_ID'] ;
				            	        option.textContent = responseJson[i]['user_ID'];
				            		     select.appendChild(option); 
						  }
		            } 
		      });
	}


function oncheck(){
	if (document.form1.otype.value == "SL")
	{
	document.form1.tp.disabled = false;
	document.getElementById('status').value = 'Pending';
	}else{
	document.form1.tp.disabled = true;
	document.getElementById('status').value = 'Executed';
	}
	}
function onCheck(){
	if (document.form2.otype1.value == "SL")
	{
	document.form2.tp.disabled = false;
	document.getElementById('status1').value = 'Pending';
	}else{
	document.form2.tp.disabled = true;
	document.getElementById('status1').value = 'Executed';
	}
	}

$(function(){
    $('.source-code').each(function(index){
        var $section = $(this);
        var code =   $('#ql').updown();
        var codee =   $('#ql1').updown();
       
       var $codePreview = $('<pre class="prettyprint lang-javascript"></pre>');
        $codePreview.text(code);
        $codePreview.text(codee);
        $section.html($codePreview);
        
       eval(code);
        eval(codee);
    });
});

